<style lang="less">
	page {
		background: url(https://static.xpandago.net/wechat/images/renwuBj-img.jpg) no-repeat center/ 100% 100% scroll ;
	}
	.main {
		padding: 0 30upx;
	}

	.main_foot {
		position: fixed;
		z-index: 1;
		width: 100%;
		bottom: 50upx;
		left: 0;
		padding: 0 30upx;
		box-sizing: border-box;
	}

	.exchange {
		width: 100%;
		height: 70upx;
		border-radius: 50upx;
		background: #000000;
		font-size: 26upx;
		font-weight: 700;
		color: #FFFFFF;
		text-align: center;
		line-height: 70upx;
	}

	.gary {
		background-color: #CCCCCC;
	}

</style>


<template>
	<view class="container">
		<view class="main">
			<view class="main_foot" @tap="onChange" v-if="flagNums == false">
				<view class="exchange">新人专享50积分</view>
			</view>
			<view class="main_foot" v-else>
				<view class="exchange gary">已领取</view>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/common/api'
	export default {
		data() {
			return {
				flagNums: true, // false 未领过
				token: '',
				http: api.httpImg, //图片前缀
			}
		},
		onLoad() {
			// this.token = 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLllJDkvK_omY7ngrnomorpppnvvIEiLCJhdWRpZW5jZSI6ImUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNlIiwidXNlcl9pZCI6Nzg1MTA0LCJjcmVhdGVkIjoxNjM5MDQ0NTM0NzUwLCJleHAiOjE2NDE2MzY1MzQsInVzZXJfZ3JvdXBfaWQiOm51bGwsInBsYXRmb3JtIjpudWxsfQ.8qDx7JFJUAIKu1gd5CQBd3CbnhXHCZkUEH8QbZZpP55gzATQp7o8B-T96Xyq-eWRYyP_RnO7pFeqyxp3Ac67fg'
			let token=this.getQueryVariable('token');
			this.token=uni.getStorageSync('token')
			let code=this.GetQueryString('code');
			var pages = getCurrentPages(); // 当前页面
			var beforePage = pages[pages.length - 1]; // 获取当前页面的对象
			var url = beforePage.route //当前页面url
			uni.setStorageSync('url', url);
			
			if(token!=false){
				uni.setStorageSync('token', token);
				this.token=token
				this.checkGetPoint()
			}else if(this.token!=null && this.token !='' && this.token != undefined){
				this.checkGetPoint()
			}else if(code!=null && code !='' && code != undefined){
				this.loginCode(code)
			}else{
				uni.showToast({
					title: '请先登录！',
					icon: 'none'
				})
				setTimeout(() => {
					uni.reLaunch({
					  url: "/pages/login/loginWechat"
					})
				}, 1000);
			}
		},
		methods: {
			//获取token与id
			getQueryVariable(variable)
			{
			       var query = window.location.search.substring(1);
			       var vars = query.split("&");
			       for (var i=0;i<vars.length;i++) {
			               var pair = vars[i].split("=");
			               if(pair[0] == variable){return pair[1];}
			       }
			       return(false);
			},
			GetQueryString(name){
			    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			    var r = window.location.search.substr(1).match(reg);
			    if(r!=null)return  unescape(r[2]); return null;
			},
			// 点击立即
			onChange() {
				this.finishTask()
			},
			// 是否已经领取过任务积分
			async checkGetPoint() {
				const [err, res] = await api.checkGetPoint({
					query: {
						type:'FollowWeChat'
					},
					header:{
						'Content-Type': 'application/json',
						'token': this.token
					}
				})
				if (res.data.code == 0) {
					this.flagNums=res.data.data
				} else {
					uni.showToast({
					 	title: res.data.msg,
					 	icon: 'none'
					 })
					if(res.data.code==200){
						uni.removeStorageSync('token');
						setTimeout(() => {
							uni.reLaunch({
							  url: "/pages/login/loginWechat"
							})
						}, 1000);
					}
				}
			},
			// 完成任务领积分
			async finishTask() {
				const [err, res] = await api.finishTask({
					query: {
						type:'FollowWeChat'
					},
					header:{
						'Content-Type': 'application/x-www-form-urlencoded',
						'token': this.token
					},
					method: 'POST'
				})
				if (res.data.code == 0) {
					this.flagNums=true
				}else{
					uni.showToast({
					 	title: res.data.msg,
					 	icon: 'none'
					 })
				}
			},
		}
	}
</script>
